/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import BO.Hotel;
import Connection.Connect;
import Exceptions.DataAccessException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author Bruno Lopes
 */
public class HotelDao{

//    public static boolean localByName(String local)
//            throws ClassNotFoundException, SQLException, FileNotFoundException,
//            IOException, DataAccessException {
//
//        ArrayList<String> toReturn = new ArrayList<String>();
//
//        String query = "SELECT * from hotel where address like"
//                + "'" + local + "'";
//
//        PreparedStatement pstmt = Connect.createConnection().prepareStatement(query);
//
//        ResultSet results = pstmt.executeQuery(query);
//
//        while (results.next()) {
//            String rset = "";
//            rset = results.toString();
//
//            toReturn.add(rset);
//        }
//
//        Connect.closeConnection(pstmt);
//
//        return toReturn.contains(local);
//    }

    public static HashMap<String, Hotel> hotelListByLocal(String local) throws
            ClassNotFoundException, SQLException, FileNotFoundException,
            IOException, DataAccessException {

        String query = "select *  from hotel where address like"
                + "'" + local + "'";

        PreparedStatement pstmt = Connect.createConnection().prepareStatement(query);

        ResultSet results = pstmt.executeQuery(query);

        HashMap<String, Hotel> toReturn = new HashMap<String, Hotel>();

        while (results.next()) {
            Hotel hotel = new Hotel();
            hotel.setId(results.getInt("id"));
            hotel.setName(results.getString("name"));
            hotel.setVacancies(results.getInt("vacancies"));
            hotel.setAddress(results.getString("address"));

            toReturn.put(hotel.getAddress(), hotel);
        }

        Connect.closeConnection(pstmt);
        return toReturn;

    }
    
    public static ArrayList<Hotel> getAll(){
        
       ArrayList<Hotel> toReturn = new ArrayList<Hotel>();
        
       String query = "select *  from hotel";

       try{
        PreparedStatement pstmt = Connect.createConnection().prepareStatement(query);

        ResultSet results = pstmt.executeQuery(query); 
        
        while (results.next()) {
            Hotel hotel = new Hotel();
            hotel.setId(results.getInt("id"));
            hotel.setName(results.getString("name"));
            hotel.setVacancies(results.getInt("vacancies"));
            hotel.setAddress(results.getString("address"));

            toReturn.add(hotel);
        }
       }catch(SQLException ex){
           System.err.println("Erro: " + ex.getMessage());
       }catch(ClassNotFoundException ex){
           System.err.println("Erro: " + ex.getMessage());
       }catch(IOException ex){
           System.err.println("Erro: " + ex.getMessage());
       }catch(DataAccessException ex){
           System.err.println("Erro no acesso à base de dados.");
       }
       
        return toReturn;
    }

    public static ArrayList<Hotel> availableHotels(){
        
       ArrayList<Hotel> toReturn = new ArrayList<Hotel>();
        
       String query = "select *  from hotel where vacancies > 0";

       try{
        PreparedStatement pstmt = Connect.createConnection().prepareStatement(query);

        ResultSet results = pstmt.executeQuery(query); 
        
        while (results.next()) {
            Hotel hotel = new Hotel();
            hotel.setId(results.getInt("id"));
            hotel.setName(results.getString("name"));
            hotel.setVacancies(results.getInt("vacancies"));
            hotel.setAddress(results.getString("address"));

            toReturn.add(hotel);
        }
       }catch(SQLException ex){
           System.err.println("Erro: " + ex.getMessage());
       }catch(ClassNotFoundException ex){
           System.err.println("Erro: " + ex.getMessage());
       }catch(IOException ex){
           System.err.println("Erro: " + ex.getMessage());
       }catch(DataAccessException ex){
           System.err.println("Erro no acesso à base de dados.");
       }
       
        return toReturn;
    }
}
